package com.zust513.controller;

import com.zust513.dto.RegionPressureDTO;
import com.zust513.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 配送压力管理控制器
 */
@Controller
@RequestMapping("/admin")
public class DeliveryPressureController {

    @Autowired
    private RegionService regionService;

    /**
     * 配送压力概览页面
     */
    @GetMapping("/delivery-pressure")
    public String deliveryPressurePage(Model model) {
        List<RegionPressureDTO> regions = regionService.getAllRegionPressure();
        model.addAttribute("regions", regions);
        model.addAttribute("view", "admin/delivery-pressure");
        return "admin/index";
    }

    /**
     * 区域配送压力详情页面
     */
    @GetMapping("/delivery-pressure/{regionId}")
    public String regionPressureDetailPage(@PathVariable Integer regionId, Model model) {
        RegionPressureDTO regionPressure = regionService.getRegionPressureById(regionId);
        if (regionPressure == null) {
            return "redirect:/admin/delivery-pressure";
        }
        model.addAttribute("region", regionPressure);
        model.addAttribute("view", "admin/region-detail");
        return "admin/index";
    }

    /**
     * 获取所有区域的配送压力 (API)
     */
    @GetMapping("/api/delivery-pressure/regions")
    @ResponseBody
    public ResponseEntity<List<RegionPressureDTO>> getAllRegionPressure() {
        List<RegionPressureDTO> regions = regionService.getAllRegionPressure();
        return ResponseEntity.ok(regions);
    }

    /**
     * 获取特定区域的配送压力详情 (API)
     */
    @GetMapping("/api/delivery-pressure/regions/{regionId}")
    @ResponseBody
    public ResponseEntity<RegionPressureDTO> getRegionPressureById(@PathVariable Integer regionId) {
        RegionPressureDTO regionPressure = regionService.getRegionPressureById(regionId);
        if (regionPressure == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(regionPressure);
    }
}
